/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Face.java
 *
 * Created on 05 22, 11, 8:16:57 AM
 */

package trialinventory2.fce;

import com.jgoodies.binding.adapter.AbstractTableAdapter;
import com.jgoodies.binding.adapter.SingleListSelectionAdapter;
import com.jgoodies.binding.list.SelectionInList;
import javax.swing.ListModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import overallPOS.modules.share.utils.TableWidthUtilities;
import trialinventory2.*;
import trialinventory2.to.ProductTO;

/**
 *
 * @author i1
 */
public class Face extends javax.swing.JPanel {

    /** Creates new form Face */
    public Face() {
        initComponents();
        myInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dlgFace1 = new trialinventory2.fce.DlgFace();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        spProducts = new javax.swing.JScrollPane();
        tblProducts = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        dspTotal = new javax.swing.JLabel();

        jLabel1.setText("Cashier Name");

        tblProducts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Product", "Quantity", "Price", "Total"
            }
        ));
        spProducts.setViewportView(tblProducts);

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        jLabel2.setText("Total");

        dspTotal.setBackground(java.awt.Color.white);
        dspTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        dspTotal.setText("999");
        dspTotal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        dspTotal.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spProducts, javax.swing.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnAdd, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dspTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(btnAdd)
                .addGap(18, 18, 18)
                .addComponent(spProducts, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(dspTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(28, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        add();
    }//GEN-LAST:event_btnAddActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private trialinventory2.fce.DlgFace dlgFace1;
    private javax.swing.JLabel dspTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JScrollPane spProducts;
    private javax.swing.JTable tblProducts;
    // End of variables declaration//GEN-END:variables

    private void myInit() {
        init_tblProducts();
    }

    private void add() {
        //todo call add dialog
    }

    private void compute() {

    }


    private TblProducts_M tblProducts_M;
    private SelectionInList tblProduct_SIL;
    private void init_tblProducts() {
        tblProduct_SIL = new SelectionInList();
        tblProducts_M = new TblProducts_M(tblProduct_SIL);
        tblProducts_M.addTableModelListener( new TableModelListener() {

            public void tableChanged(TableModelEvent e) {
                compute();
            }


        });

        tblProducts.setModel(tblProducts_M);
        tblProducts.setSelectionModel(new SingleListSelectionAdapter(
                tblProduct_SIL.getSelectionHolder()));
        tblProducts.setRowHeight(25);

        TableWidthUtilities.setColumnWidth(tblProducts, 1, 100);
        TableWidthUtilities.setColumnWidth(tblProducts, 2, 100);
        TableWidthUtilities.setColumnWidth(tblProducts, 3, 100);

        TableWidthUtilities.setColumnRightRenderer(tblProducts, 1);
        TableWidthUtilities.setColumnRightRenderer(tblProducts, 2);
        TableWidthUtilities.setColumnRightRenderer(tblProducts, 3);
    }

    private static class TblProducts_M extends AbstractTableAdapter {

        private static final String[] COLUMNS = {
          "Product", "Price", "Quantity", "Total"
        };

        public TblProducts_M(ListModel listModel) {
            super(listModel, COLUMNS);
        }


        public Object getValueAt(int col, int row) {
            ProductTO to = (ProductTO) getRow(row);

            switch( col ) {
                case 0:
                    return to.product;
                case 1:
                    return to.price;
                case 2:
                    return to.getQuantity();
                default:
                    return to.getTotal();
            }
        }

    }


}
